//
//@File    :	15.11_error4.cpp
//@Time    :	2022/03/10 14:06:23
//@Auther  :	Sun Sheng
//@Contact :	970340843@qq.com
//@Desc    :	using exception classes
//

#include <iostream>
#include <cmath>
#include "15.10_exc_mean.h"

double hmean(double a,double b);
double gmean(double a,double b);

int main()
{
    using std::cout;
    using std::cin;
    using std::endl;

    double x,y,z;

std::cout<<"Enter two numters: ";
    while (std::cin>>x>>y)
    {
        try{ //start of try block
            z=hmean(x,y);
            cout<<"Harmonice mean of "<<x
                <<" and "<<y<<"is"<<z<<endl;
            cout<<"Geometric mean of "<<x
                <<" and "<<y<<"is"<<gmean(x,y)<<endl;
        }//end of try block
        catch(bad_hmean & bg)//start of catch block
        {
            bg.mesg();
            cout<<"Try again.\n";
            continue;//产生异常时跳过循环
        }
        catch(bad_gmean & hg)
        {
            cout<<hg.mesg();
            cout<<"Values used: "<<hg.v1<<", "<<hg.v2<<endl;
            cout<<"Sorry,you don't get to paly any more.\n";
            break;//产生异常时结束循环
        }//end of catch block
    }
    cout<<"Bye!\n";
    system("pause");
    return 0;
}

double hmean(double a,double b){
    if (a==-b)
    {
       throw bad_hmean(a,b);
    }
    return 2.0*a*b/(a+b);
}

double hmean(double a,double b){
    if (a<0||b<0)
    {
       throw bad_gmean(a,b);
    }
    return std::sqrt(a*b);
}

